/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.action.standard;

import com.daikit.daikit4gxt.client.DkMain;
import com.daikit.daikit4gxt.client.action.BaseAction;
import com.daikit.daikit4gxt.client.controller.BaseMainController;
import com.daikit.daikit4gxt.client.screen.Screen;


/**
 * Get the action reloading current screen. Called for example in
 * {@link BaseMainController#refreshScreen(boolean, Object...)} or after connection success / failure.
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class BaseReloadCurrentScreenAction
{

	/**
	 * Get the reload current screen action with invalidateUI action at the end of the chain.
	 * 
	 * @param force
	 *           a boolean indicating whether the layout refresh has to be forced
	 * @param optionalArgs
	 *           optional argument to pass to reload screen function
	 * @return the reload current screen action
	 */
	public static BaseAction<?> get(final boolean force, final Object... optionalArgs)
	{
		return get2(force, true, optionalArgs);
	}

	/**
	 * Get the reload current screen action.
	 * 
	 * @param force
	 *           a boolean indicating whether the layout refresh has to be forced
	 * @param invalidateUi
	 *           indicating whether to add invalidateUI action at the end of the chain
	 * @param optionalArgs
	 *           optional argument to pass to reload screen function
	 * @return the reload current screen action
	 */
	public static BaseAction<?> get2(final boolean force, final boolean invalidateUi, final Object... optionalArgs)
	{
		final Screen currentScreen = DkMain.controller().getCurrentScreen();
		BaseAction<?> reloadScreenAction = null;
		if (currentScreen != null)
		{
			reloadScreenAction = DkMain.controller().getCurrentScreen().getBaseReloadScreenAction(force, optionalArgs);
			final BaseAction<?> endOfReloadAction = DkMain.controller().getCurrentScreen()
					.getBaseEndOfReloadScreenAction(force, optionalArgs);
			if (reloadScreenAction == null)
			{
				reloadScreenAction = endOfReloadAction;
			}
			else if (endOfReloadAction != null)
			{
				reloadScreenAction.setLastChainAction(endOfReloadAction);
			}
			if (invalidateUi)
			{
				final BaseAction<Void> invalidateUiAction = BaseInvalidateUiAction.get(false);
				if (reloadScreenAction != null)
				{
					reloadScreenAction.setLastChainAction(invalidateUiAction);
				}
				else
				{
					reloadScreenAction = invalidateUiAction;
				}
			}
		}
		return reloadScreenAction;
	}
}
